package com.yorick.common.error;

import com.alibaba.fastjson.JSONObject;
import com.yorick.common.util.ResponseWriter;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.util.function.Supplier;

@Slf4j
public class YorickAssert {

    public static void validate(boolean condition, Supplier<String> flag, Supplier<String> msg) {
        if (!condition) {
            throw new YorickException(flag.get(), msg.get());
        }
    }

    public static void validate(boolean condition, HttpServletResponse response,
                                Supplier<String> flag, Supplier<String> msg) {
        if (!condition) {
            JSONObject result = new JSONObject();
            result.put("flag", flag.get());
            result.put("msg", msg.get());
            ResponseWriter.responseOutWithJson(response, result);
        }
    }
}
